#!/bin/bash

#------------------------------------------------------------------------------
#Filename:	75336-nfs.sh
#Version:	1.1
#Date:		2013/11/19
#Author:	yuanhui.shi
#Email:		yuanhui.shi@cs2c.com.cn
#Summary:	02.04网络服务-05NFS-01字符模式-02添加共享目录-02添加主机为指定域名
#Notes:		NFS配置——添加主机为指定域名
#Copyright:	China Standard Software Co.,Ltd.
#History:
#		Version 1.0,2013/11/19
#		-The first one
#		Version 1.1,2013/11/20
#		-The second one
#		-use GetValue to access ServerIP USER PASSWORD
#               -删除配置服务器完成后，客户端showmount产看共享目录 
#               -设置完成域名后，重启nfs服务
#               -cleandata函数内增加删除添加的域名操作
#-----------------------------------------------------------------------------


PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

source ../../../lib/Echo.sh
source ./lib/Ssh.sh
source ../../../lib/XmlParse.sh
source ./lib/cmd.sh

function CleanData ()
{
  umount /mnt/rw &> /dev/null
  umount /mnt/ro &> /dev/null
  EchoInfo "关闭服务器端的nfs服务，删除共享目录"
  Ssh "${USER}" "${ServerIP}" "${PASSWORD}" "command -v systemctl && /bin/systemctl stop nfs.service || /sbin/service nfs stop"
  Ssh "${USER}" "${ServerIP}" "${PASSWORD}" "rm -rf /nfs-test"
  Ssh "${USER}" "${ServerIP}" "${PASSWORD}" "echo '' >/etc/exports"
  Ssh "${USER}" "${ServerIP}" "${PASSWORD}" "sed -i /${LOCAL_IP}/d /etc/hosts "
#  Ssh "${AuthUSER}" "${AuthIP}" "${AuthPASSWORD}" "umount /mnt/rw"
#  Ssh "${AuthUSER}" "${AuthIP}" "${AuthPASSWORD}" "umount /mnt/ro"
#  Ssh "${AuthUSER}" "${AuthIP}" "${AuthPASSWORD}" "rm -rf /mnt/*"
  rm -rf  /mnt/*
  EchoInfo "75336-nfs.sh执行完毕"
}

trap "CleanData" EXIT INT
if [ -z "$SFROOT" ]
then
    echo "SFROOT is null"
    exit 1
fi

DATA_DIR="${SFROOT}/testcases/Network_Service"
DATA_FILE="config.xml"

XmlParse ${DATA_DIR}/${DATA_FILE}

#GetValue REHOST ReHost
#GetValue RUSERNAME UserName
#GetValue PASSWORD RootPasswd

GetValue ServerIP ReHost
GetValue USER UserName
GetValue PASSWORD RootPasswd

#USER=${RUSERNAME}
#ServerIP=${REHOST}
#PASSWORD=${PASSWORD}

AuthDomain=`hostname`


#DATA_DIR="./data"
#DATA_FILE="75336-nfs.xml"
#
#XmlParse ${DATA_DIR}/${DATA_FILE}
#
#GetValue USER USER
#GetValue ServerIP ServerIP
#GetValue PASSWORD PASSWORD
#GetValue AuthUSER AuthUSER
#GetValue AuthIP AuthIP
#GetValue AuthPASSWORD AuthPASSWORD
#GetValue AuthDomain  AuthDomain

#远程配置服务器
EchoInfo "远程配置服务器${ServerIP}"

Scp "${USER}" "${ServerIP}" "${PASSWORD}" "./nfs-config.sh" "/root/"

Ssh "${USER}" "${ServerIP}" "${PASSWORD}" "sed -i s/*/${AuthDomain}/g /root/nfs-config.sh"

Ssh "${USER}" "${ServerIP}" "${PASSWORD}" "sh nfs-config.sh"

Ssh "${USER}" "${ServerIP}" "${PASSWORD}" "echo 查看服务器端nfs共享目录清单"

Ssh "${USER}" "${ServerIP}" "${PASSWORD}"  "showmount -e 127.0.0.1"

Ssh "${USER}" "${ServerIP}" "${PASSWORD}"  "rm -rf /root/nfs-config.sh"

#客户端是否可以查看到nfs共享目录
#EchoInfo "客户端是否能够查看到nfs共享目录"
#stop_daemon iptables  
#
#test -n "`showmount -e ${ServerIP} |grep /nfs-test/readonly`" -a -n "`showmount -e ${ServerIP} |grep /nfs-test/rw`" 
#EchoResult "客户端可以看见nfs共享目录"

#---------------------------------配置本机为授权客户端，并将域名添加到nfs服务器端/etc/hosts------------------------------------

LOCAL_IP=`ifconfig | grep 'inet ' | grep -m 1 -v '127.0.0.1'  | awk '{print $2}' | tr -d "addr:"`
Ssh "${USER}" "${ServerIP}" "${PASSWORD}" "sed -i /${LOCAL_IP}/d /etc/hosts; echo ${LOCAL_IP} ${AuthDomain} >> /etc/hosts"

#设置完成域名后，重启nfs服务
Ssh "${USER}" "${ServerIP}" "${PASSWORD}" "test -f /bin/systemctl  && /bin/systemctl restart nfs.service || /sbin/service nfs restart"

#---------------------------------授权客户端------------------------------------
#确定客户端为授权客户端
EchoInfo "确认客户端为授权客户端"
#Ssh "${AuthUSER}" "${AuthIP}" "${AuthPASSWORD}" "test "`hostname`" == "${AuthDomain}""
[ `hostname` = ${AuthDomain} ]
EchoResult "该客户端为授权客户端"

mount | grep ${ServerIP}:/nfs-test/readonly | grep /mnt/ro
[ $? -eq 0 ] && umount -f /mnt/ro
mount | grep ${ServerIP}:/nfs-test/rw | grep /mnt/rw
[ $? -eq 0 ] && umount -f /mnt/rw

#授权客户端在/mnt目录下新建两个目录：rw和ro
EchoInfo "授权客户端新建目录"
#Ssh "${AuthUSER}" "${AuthIP}" "${AuthPASSWORD}" "mkdir /mnt/ro"
#Ssh "${AuthUSER}" "${AuthIP}" "${AuthPASSWORD}" "mkdir /mnt/rw"
[ -d /mnt/ro ] || mkdir /mnt/ro
[ -d /mnt/rw ] || mkdir /mnt/rw

#授权客户端挂载只读目录/nfs-test/readonly
#Ssh "${AuthUSER}" "${AuthIP}" "${AuthPASSWORD}" "mount ${ServerIP}:/nfs-test/readonly /mnt/ro"
mount ${ServerIP}:/nfs-test/readonly /mnt/ro
 
EchoInfo "检验授权客户端能否显示目录"
#Ssh "${AuthUSER}" "${AuthIP}" "${AuthPASSWORD}" "ls /mnt/ro |grep file1"
ls /mnt/ro |grep file1
EchoResult "授权客户端可以查看只读目录"


#授权客户端挂载读写目录/nfs-test/rw
#Ssh "${AuthUSER}" "${AuthIP}" "${AuthPASSWORD}" "mount ${ServerIP}:/nfs-test/rw /mnt/rw"
mount ${ServerIP}:/nfs-test/rw /mnt/rw

EchoInfo "检验授权客户端能否在读写目录下进行写操作"
#Ssh "${AuthUSER}" "${AuthIP}" "${AuthPASSWORD}" "echo 'This is a test!' > /mnt/rw/file1"
echo 'This is a test!' > /mnt/rw/file1
EchoResult "授权客户端在读写目录下进行写操作"

#--------------------------------非授权客户端-----------------------------------
#判断本地客户端属性
#EchoInfo "判断本地客户端属性"
#LocalDomain=`hostname`
#LocalIP=`ifconfig eth0 |grep "inet addr" |sed s/^.*addr://g |sed s/Bcast.*$//g`
#if [ "${LocalIP}" == "${ServerIP}" ] || [ "${LocalDomain}" == "${AuthDomain}" ]
#then 
#    echo "为了方便测试，请不要指定本机器为服务器端和授权客户端"
#    exit 1
#fi
#
##非授权客户端在/mnt目录下新建两个目录：rw和ro
#EchoInfo "非授权客户端新建目录"
#mkdir /mnt/ro
#mkdir /mnt/rw
#
##非授权客户端挂载只读目录/nfs-test/readonly
#mount ${ServerIP}:/nfs-test/readonly /mnt/ro &>/dev/null
#
#if [ $? -ne 0 ];then
#    echo "PASS|非授权客户端不能挂载只读目录"
#else
#    echo "FAIL|非授权客户端不能挂载只读目录"
#fi 
#
##非授权客户端挂载读写目录/nfs-test/rw
#mount ${ServerIP}:/nfs-test/rw /mnt/rw &>/dev/null
#
#if [ $? -ne 0 ];then
#    echo "PASS|非授权客户端不能挂载读写目录"
#else
#    echo "FAIL|非授权客户端不能挂载读写目录"
#fi
